package com.sqfw.project.knowledge.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.sqfw.project.knowledge.domain.KlHasNotwrong;
import com.sqfw.project.knowledge.domain.query.KlHasNotwrongQuery;
import com.sqfw.project.knowledge.domain.vo.KlHasNotwrongCountVo;
import com.sqfw.project.knowledge.service.KlHasNotwrongService;
import com.sqfw.project.system.domain.SysDictData;
import com.sqfw.project.system.service.ISysDeptService;
import com.sqfw.project.system.service.ISysDictDataService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 【查无纠错数据表】Controller
 * 
 * @author tangxt
 * @date 2023-06-09
 */
@RestController
@RequestMapping("/klHasNotwrong")
public class KlHasNotwrongController extends BaseController {

    @Autowired
    private KlHasNotwrongService klHasNotwrongService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询查无/纠错数据列表
     */
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @GetMapping("/list")
    public TableDataInfo list(KlHasNotwrong klHasNotwrong) {
        startPage();
        List<KlHasNotwrong> list = klHasNotwrongService.selectKlHasNotwrongList(klHasNotwrong);
        return getDataTable(list);
    }

    /**
     * 导出查无/纠错数据列表
     */
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @Log(title = "查无/纠错数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KlHasNotwrong klHasNotwrong) {
        List<KlHasNotwrong> list = klHasNotwrongService.selectKlHasNotwrongList(klHasNotwrong);
        ExcelUtil<KlHasNotwrong> util = new ExcelUtil<KlHasNotwrong>(KlHasNotwrong.class);
        util.exportExcel(response, list, "查无/纠错数据数据");
    }

    /**
     * 获取查无/纠错数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(klHasNotwrongService.selectKlHasNotwrongById(id));
    }

    /**
     * 新增查无/纠错数据--添加
     * @param klHasNotwrong
     * @return
     */
    @ApiOperation("新增查无/纠错数据")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong:add')")
    @Log(title = "新增查无/纠错数据", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody @Valid KlHasNotwrong klHasNotwrong) {
        return toAjax(klHasNotwrongService.insertKlHasNotwrong(klHasNotwrong));
    }

    /**
     * 修改查无/纠错数据
     * @param klHasNotwrong
     * @return
     */
    @ApiOperation("修改查无/纠错数据")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @Log(title = "修改查无/纠错数据", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    public AjaxResult edit(@RequestBody KlHasNotwrong klHasNotwrong) {
        return toAjax(klHasNotwrongService.updateKlHasNotwrong(klHasNotwrong));
    }

    /**
     * 修改查无/纠错数据,以及附件
     * @param klHasNotwrong
     * @return
     */
    @ApiOperation("修改查无/纠错数据")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @Log(title = "修改查无/纠错数据,以及附件", businessType = BusinessType.UPDATE)
    @PostMapping("updateKlHasNotwrongFile")
    public AjaxResult updateKlHasNotwrongFile(@RequestBody KlHasNotwrong klHasNotwrong) {
        return toAjax(klHasNotwrongService.updateKlHasNotwrongFile(klHasNotwrong));
    }

    /**
     * 删除查无/纠错数据
     */
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @Log(title = "查无/纠错数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(klHasNotwrongService.deleteKlHasNotwrongByIds(ids));
    }

    /**
     * 添加查无纠错数据
     * */

    /**
     * 查询查无和纠错原因的列表--分组模式
     * @return
     * */
    @ApiOperation("分组查询-查无/纠错原因")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @GetMapping("queryKlHasReasonAll")
    public AjaxResult queryKlHasReasonAll() {
        List<Map<String, Object>> list = new ArrayList<>();
        SysDictData dictData = new SysDictData();
        // 查无原因(添加查无数据时选择)：knowledge_no_reason_found
        dictData.setDictType("knowledge_no_reason_found");
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataList(dictData);
        Map<String, Object> map = new HashMap<>();
        map.put("label", "查无原因");
        map.put("options", sysDictDataList);
        list.add(map);
        // 纠错原因(添加纠错数据时选择): knowledge_correction_reasons
        dictData.setDictType("knowledge_correction_reasons");
        List<SysDictData> dictDataList = dictDataService.selectDictDataList(dictData);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("label", "纠错原因");
        map1.put("options", dictDataList);
        list.add(map1);
        return AjaxResult.success(list);
    }

    /**
     * 审核
     * 第一层审核：提交待审核--->待处理
     * 第二层审核：待处理--->办结待审核,提交审核退回
     * 第三层审核：办结待审核--->已归档,办结审核退回
     * @param klHasNotwrong
     * @return
     * */
    @ApiOperation("审核查无/纠错数据")
    @Log(title = "审核查无/纠错数据", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @PostMapping("/toExamineKlHasNotwrong")
    public AjaxResult toExamineKlHasNotwrong(HttpServletRequest request, @RequestBody KlHasNotwrong klHasNotwrong) {
        if (StringUtils.isBlank(klHasNotwrong.getId()) || StringUtils.isBlank(klHasNotwrong.getKlHasStatus())
                || StringUtils.isBlank(klHasNotwrong.getAuditStatus()) || StringUtils.isBlank(klHasNotwrong.getReviewComment())) {
            return AjaxResult.error("参数没有传递！");
        }
        return klHasNotwrongService.toExamineKlHasNotwrong(request, klHasNotwrong);
    }

    /**
     * 提交待审核--->待处理---->
     *
     *
     *
     * */

    /**
     * 分页查询-根据部门查询/根据部门+in 状态查询
     * @param query
     * @return
     * */
    @ApiOperation("分页查询-根据部门查询")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @GetMapping("queryKlHasNotwrongByDept")
    public TableDataInfo queryKlHasNotwrongByDept(KlHasNotwrongQuery query) {
        startPage();
        List<KlHasNotwrong> list = klHasNotwrongService.queryKlHasNotwrongByDept(query);
        return getDataTable(list);
    }

    /**
     * 分页查询--根据in查询 状态
     * @param query
     * @return
     * */
    @ApiOperation("分页查询")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @GetMapping("queryKlHasNotwrongList")
    public TableDataInfo queryKlHasNotwrongList(KlHasNotwrongQuery query) {
        startPage();
        List<KlHasNotwrong> list = klHasNotwrongService.queryKlHasNotwrongList(query);
        return getDataTable(list);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     * */
    @ApiOperation("根据id查询")
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @GetMapping("getKlHasNotwrong")
    public  AjaxResult getKlHasNotwrong(@RequestParam("id") String id) {
        return success(klHasNotwrongService.selectKlHasNotwrongById(id));
    }

    /**
     * 承办单位点击处理，标记该查无审核中
     * 承办单位取消处理，去掉审核中（修改成未审核）
     * @param klHasNotwrong id
     * @return
     * */
    @Log(title = "承办单位审核中", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @PostMapping("updateRead")
    public AjaxResult updateRead(@RequestBody KlHasNotwrong klHasNotwrong) {
        if (StringUtils.isBlank(klHasNotwrong.getId()) || klHasNotwrong.getIsRead() == null) {
            return AjaxResult.error("id/状态必传！");
        }
        return klHasNotwrongService.updateRead(klHasNotwrong);
    }

    /**
     * 撤回（社区服务中心，派发给承办单位，发现错误了，撤回该查无/纠错）
     * @param klHasNotwrong 查无/纠错id
     * @return
     * */
    @Log(title = "撤回", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @PostMapping("revokeKlHasNotwrong")
    public AjaxResult revokeKlHasNotwrong(@RequestBody KlHasNotwrong klHasNotwrong) {
        if (StringUtils.isBlank(klHasNotwrong.getId())) {
            return AjaxResult.error("id必传！");
        }
        return klHasNotwrongService.revokeKlHasNotwrong(klHasNotwrong);
    }

    /**
     * 导出查无/纠错数据
     * @param response
     * @param query
     * */
    @ApiOperation("导出查无/纠错数据")
    @Log(title = "导出查无/纠错数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @PostMapping("exportQueryList")
    public void exportQueryList(HttpServletResponse response, KlHasNotwrongQuery query) {
        List<KlHasNotwrong> list = klHasNotwrongService.queryKlHasNotwrongList(query);
        ExcelUtil<KlHasNotwrong> util = new ExcelUtil<KlHasNotwrong>(KlHasNotwrong.class);
        util.exportExcel(response, list, "查无纠错数据");
    }

    /**
     * 根据部门导出查无/纠错数据
     * @param response
     * @param query
     * */
    @ApiOperation("根据部门导出查无/纠错数据")
    @Log(title = "根据部门导出查无/纠错数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('klHasNotwrong')")
    @PostMapping("exportQueryListByDept")
    public void exportQueryListByDept(HttpServletResponse response, KlHasNotwrongQuery query) {
        List<KlHasNotwrong> list = klHasNotwrongService.queryKlHasNotwrongByDept(query);
        ExcelUtil<KlHasNotwrong> util = new ExcelUtil<KlHasNotwrong>(KlHasNotwrong.class);
        util.exportExcel(response, list, "查无纠错数据");
    }



    /**
     * 统计查无/纠错
     * 默认条件：-1 已归档，三级承办单位（处理单位）不能为空
     * 选择条件：主办单位（部门-只能选择最后一级），发起时间范围，姓名（真实姓名）
     * @param query
     * @return
     * */
    @ApiOperation("统计查无/纠错")
    @PreAuthorize("@ss.hasAnyPermi('klHasNotwrong,countKlHasNotwrong')")
    @PostMapping("countKlHasNotwrong")
    public AjaxResult countKlHasNotwrong(@RequestBody KlHasNotwrongQuery query) {
        List<KlHasNotwrongCountVo> countVoList = klHasNotwrongService.countKlHasNotwrong(query);
        if (CollectionUtils.isEmpty(countVoList)) {
            return AjaxResult.success("没有数据", countVoList);
        }
        return AjaxResult.success(countVoList);
    }






}
